package com.ruiysoft.controller.mdse;

import com.alibaba.fastjson.JSONObject;
import com.ruiysoft.controller.integralmdse.vo.InnrIntegralMdseVo;
import com.ruiysoft.controller.mdse.util.MdseVoUtil;
import com.ruiysoft.controller.mdse.vo.OrderSkuListVo;
import com.ruiysoft.controller.mdse.vo.SkuListVo;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.mdse.IMerchandiseManage;
import com.ruiysoft.service.mdse.bean.MerchandiseSkuDto;
import com.ruiysoft.service.mdse.bean.MerchandiseStoceEnvelope;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-11-03T17:11:33.095+08:00")

@RestController
public class InnrApiController implements InnrApi {

    private static final Logger log = LoggerFactory.getLogger(InnrApiController.class);

    private final IMerchandiseManage mdseManage;

    @org.springframework.beans.factory.annotation.Autowired
    public InnrApiController(IMerchandiseManage mdseManage) {
        this.mdseManage = mdseManage;
    }

    @Override
    public Result<Void> innrMdseDerBatchPost(@ApiParam(value = ""  )  @Valid @RequestBody List<MerchandiseStoceEnvelope> skuNums) {
        try {
            log.debug("innr扣减库存--{}",JSONObject.toJSONString(skuNums));
            mdseManage.derBatchQuantity(skuNums);
            return ResultGenerator.genSuccessResult();
        } catch (Exception e) {
            log.error("innr扣减库存---{}",JSONObject.toJSONString(skuNums), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }



    @Override
    public Result<Map<Long,SkuListVo>> innrMdseSkuListCartPost(@ApiParam(value = ""  )  @Valid @RequestBody List<Long> skuIds, @RequestParam(required = false) Integer mLv) {
        try {
            Map<Long,InnrIntegralMdseVo> innrIntegralMdseVoMap = mdseManage.findIntegralMdseMapBySkuIdsAndMLv(skuIds,mLv);
            List<MerchandiseSkuDto> dtoList = mdseManage.findSkuDtoListByCondition(skuIds);
            List<SkuListVo> list = new ArrayList<>();
            Map<Long,SkuListVo> map = dtoList.stream().map(skuDto->{
                SkuListVo vo = null;
                try {
                    InnrIntegralMdseVo integralMdseVo = innrIntegralMdseVoMap.get(skuDto.getSku().getSkuId());
                    vo = MdseVoUtil.skuDtoToWxSkuListVo(skuDto, integralMdseVo, null);

                } catch (Exception e) {
                    log.error("skuDtoToWxSkuListVo--{}",JSONObject.toJSONString(skuDto), e);
                }
                return vo;
            }).collect(Collectors.toMap(vo->vo.getSkuId(),vo->vo));
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            log.error("innr购物车查询商品信息---{}",JSONObject.toJSONString(skuIds), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result<Map<Long,OrderSkuListVo>> innrMdseSkuListOrderPost(@ApiParam(value = ""  )  @Valid @RequestBody List<Long> skuIds, @RequestParam(required = false) Integer mLv) {
        try {
            List<MerchandiseSkuDto> dtoList = mdseManage.findSkuDtoListByCondition(skuIds);
            Map<Long,InnrIntegralMdseVo> innrIntegralMdseVoMap = mdseManage.findIntegralMdseMapBySkuIdsAndMLv(skuIds,mLv);
            Map<Long,OrderSkuListVo> map = dtoList.stream().map(skuDto->{
                OrderSkuListVo vo = null;
                try {
                    InnrIntegralMdseVo integralMdseVo = innrIntegralMdseVoMap.get(skuDto.getSku().getSkuId());
                    vo = MdseVoUtil.skuDtoToOrderSkuListVo(skuDto,integralMdseVo);
                } catch (Exception e) {
                    log.error("skuDtoToOrderSkuListVo--{}",JSONObject.toJSONString(skuDto), e);
                }
                return vo;
            }).collect(Collectors.toMap(vo->vo.getSkuId(),vo->vo));
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            log.error("innr订单查询商品信息---{}",JSONObject.toJSONString(skuIds), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

}
